package com.zwj.ruochuapiclientsdk.client;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.zwj.ruochuapiclientsdk.model.*;
import com.zwj.ruochuapiclientsdk.utils.SignUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

/**
 * 调用第三方接口的客户端
 *
 * @author zwj
 */
@SuppressWarnings("all")
public class RuoChuApiClient {

    private static final String GATEWAY_HOST = "http://127.0.0.1:9999";
    private static final String PATH_PREFIX = GATEWAY_HOST + "/api/";

    private final String accessKey;
    private final String secretKey;

    public RuoChuApiClient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    // 通用添加请求头
    private Map<String, String> addHeaders(String body) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("accessKey", accessKey);
        hashMap.put("nonce", String.valueOf(new Random().nextInt()));
        hashMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
        hashMap.put("sign", SignUtils.genSign(body, secretKey));
        hashMap.put("body", body);
        return hashMap;
    }

    public Map<?, ?> getDailyEnglishSentence() {
        HttpResponse execute = HttpRequest.get(PATH_PREFIX + "daily-english-sentence")
                .addHeaders(addHeaders(null))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);
    }

    public Map<?, ?> calculateBMI(BMIReq bmiReq) {
        if (Objects.isNull(bmiReq)) {
            throw new RuntimeException("参数错误");
        }
        String body = JSONUtil.toJsonStr(bmiReq);
        HttpResponse execute = HttpRequest.post(PATH_PREFIX + "bmi")
                .body(body)
                .addHeaders(addHeaders(body))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);
    }

    public Map<?, ?> predictHeight(PredictedHeightReq predictedHeightReq) {
        if (Objects.isNull(predictedHeightReq)) {
            throw new RuntimeException("参数错误");
        }
        String body = JSONUtil.toJsonStr(predictedHeightReq);
        HttpResponse execute = HttpRequest.post(PATH_PREFIX + "predict-height")
                .body(body)
                .addHeaders(addHeaders(body))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        System.out.println(respBody);
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);
    }

    public Map<?, ?> calculateQQFortune(QQFortuneReq qqFortuneReq) {
        if (Objects.isNull(qqFortuneReq)) {
            throw new RuntimeException("参数错误");
        }
        String body = JSONUtil.toJsonStr(qqFortuneReq);
        HttpResponse execute = HttpRequest.post(PATH_PREFIX + "qq-fortune")
                .body(body)
                .addHeaders(addHeaders(body))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);

    }

    public Map<?, ?> calculateBirthInfo(BirthAssistantReq birthAssistantReq) {
        if (Objects.isNull(birthAssistantReq)) {
            throw new RuntimeException("参数错误");
        }
        String body = JSONUtil.toJsonStr(birthAssistantReq);
        HttpResponse execute = HttpRequest.post(PATH_PREFIX + "birth-assistant")
                .body(body)
                .addHeaders(addHeaders(body))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);

    }

    public Map<?, ?> convertCurrency(ConversionReq conversionReq) {
        if (Objects.isNull(conversionReq)) {
            throw new RuntimeException("参数错误");
        }
        String body = JSONUtil.toJsonStr(conversionReq);
        HttpResponse execute = HttpRequest.post(PATH_PREFIX + "convertCurrency")
                .body(body)
                .addHeaders(addHeaders(body))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);

    }

    public byte[] generateAvatar() {
        HttpResponse execute = HttpRequest.get(PATH_PREFIX + "avatar")
                .addHeaders(addHeaders(null))
                .execute();
        String respBody = execute.body();
        return respBody.getBytes(StandardCharsets.UTF_8);

    }

    public Map<?, ?> getUnfashionableLoveTalk() {
        HttpResponse execute = HttpRequest.get(PATH_PREFIX + "unfashionable-love-talk")
                .addHeaders(addHeaders(null))
                .execute();
        String respBody = execute.body();
        if (StrUtil.isBlank(respBody)) {
            throw new RuntimeException("调用失败");
        }
        if (!respBody.startsWith("{")) {
            throw new RuntimeException("调用失败");
        }
        return JSONUtil.parseObj(respBody);

    }
}
